package practice.library.gremlin_scala.example

import com.thinkaurelius.titan.graphdb.vertices.{CacheVertex, StandardVertex}
import gremlin.scala._
import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet
import org.apache.tinkerpop.gremlin.structure.T
import utility.CustomLogger

import scala.util.Try

/**
  * Created by lovepocky on 16/6/22.
  */
object OrdinaryExample extends CustomLogger {

	import gremlin.scala.{__ => _}
	import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__
	import practice.library.gremlin_scala.utility.CustomGremlinUtility._

	def main(args: Array[String]) {
		//exampleOfNorthWind()

		//TestOf160.match1()

		TestOfTinkerGraphBuildIn.match3()
	}

	def exampleOfNorthWind() = {

		val g = ConsoleExample.graphOfNorthWind()

		val exampleUrl = "http://sql2gremlin.com/"
		loggers.console.info(s"Examples from: $exampleUrl")

		loggers.console.info(s"Left Join from $exampleUrl")
		g.V.hasLabel("customer").`match`(
			__.as("c").values("customerId").as("customerId"),
			__.as("c").out("ordered").count().as("orders"))
			.select("customerId", "orders")
			.toList().foreach(println)

		loggers.console.info("Left Join of mine")
		g.V.hasLabel("customer").`match`(
			__.as("c").values("customerId").as("customerId"),
			__.as("c").out("ordered").as("orders"))
			.select("customerId", "orders")
			.toList().foreach(println)



		/*g.V().hasLabel("customer").union(
		filter {it.get().value("company")[0] == "A"},
		filter {it.get().value("company")[0] == "E"}).values("company")*/


		loggers.console.info("Other Custom Examples")

		loggers.console.info("create/drop/commit")
		val tempVertex = g.addVertex("temp_vertex")
		g.tx().commit() //important
		g.V().hasLabel("temp_vertex").drop().iterate()
		g.tx().commit()

	}

	def exampleOf160() = {
		import gremlin.scala._
		val g = ConsoleExample.graphOfCassandraWith160()
	}

	def myTestOf160(): Unit = {
		import gremlin.scala._
		val g = ConsoleExample.graphOfCassandraWith160()

		g.V.has(Key[String]("name"), "rome").as("a").in("location").hasLabel("hotel").count().as("b").select().toList().foreach(println)
		g.V.has(Key[String]("name"), "rome").as("a").in("location").hasLabel("hotel").as("b").count().select().toList().foreach(println)

		//g.V.has(Key[String]("name"),"rome").in("location").local(_.label().order().limit(2)).valueMap()

		val temp = g.V.has(Key[String]("name"), "rome").in("location").group().by(T.label).head()
	}

	object TestOf160 {

		import gremlin.scala._

		val g = ConsoleExample.graphOfCassandraWith160()

		def group() = {

			val temp = g.V.has(Key[String]("name"), "rome").in("location").group().by(T.label).head()
			println(temp)

			import scala.collection.JavaConverters._
			val temp2 = temp.asInstanceOf[java.util.Map[String, BulkSet[CacheVertex]]].asScala.mapValues(_.asScala.toList.take(3))

			//CacheVertex : queryCache
			temp2.foreach { it =>
				println(it._1)
				it._2.foreach(it => println(it.valueMap("name", "name_chs")))
			}
			g.tx.rollback()
			g.graph.close()
		}

		def where() = {
			val v_rome = g.V.has(Key[String]("name"), "rome").head
			g.V.has(Key[String]("name"), "rome").in("location").where(_.out("location").is(v_rome)).toList()

			g.close()
		}

		def match1() = {
			loggers.console.info("example Of mine")
			import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__
			val a = g.V.`match`(__.as("a").hasLabel("city")).head

			g.close()
		}
	}

	object TestOfTinkerGraphBuildIn {
		val g = ConsoleExample.graphOfTinkerGraphBuildIn()

		def match1() = {
			loggers.console.info("example from : http://tinkerpop.apache.org/docs/3.0.2-incubating/#match-step")
			val a = g.V.`match`(
				__.as("a").out("created").as("b"),
				__.as("b").has("name", "lop"),
				__.as("b").in("created").as("c"),
				__.as("c").has("age", 29)
			).select("a", "c").by("name")
				.toList()

			a.foreach(println)
		}

		def match2() = {
			val a = g.V.`match`(
				__.as("a").out("created").as("b")
				//__.as("b").has("name", "lop"),
				//__.as("b").in("created").as("c"),
				//__.as("c").has("age", 29)
			).select("a", "b").by("name")
				.toList() //.foreach(println)

			a.foreach(println)
		}

		def match3() = {
			val a = g.V.`match`(
				__.as("a")
			).select("a").by("name")
				.toList()

			a.foreach(println)
		}

		def match4() = {
			val a = g.V.`match`(
				__.as("a")
			).select("a").by("name")
				.toList()

			a.foreach(println)
		}

	}

	object Model {

		case class SimpleVertex(name: String)

		case class SimpleVertexWithOption(name: String, option: Option[String])

		@label("vertex_label")
		case class SimpleVertexWithLabel(name: String)


		import org.joda.time.DateTime

		//supported (custom serializer)
		@label("edge_label_time")
		case class SimpleTimeEdge(dateTime: DateTime)

		@label("edge_label")
		case class SimpleEdge()

	}

}

